package com.loubii.sunshineSchool.httpRequest;

import android.content.Context;
import android.widget.Toast;

import com.loubii.sunshineSchool.app.MainApplication;
import com.loubii.sunshineSchool.pojo.ActivityContainer;
import com.loubii.sunshineSchool.pojo.ActivityDetail;
import com.loubii.sunshineSchool.pojo.ApplyDetail;
import com.loubii.sunshineSchool.pojo.ChatItem;
import com.loubii.sunshineSchool.pojo.MsgItem;
import com.loubii.sunshineSchool.pojo.User;
import com.loubii.sunshineSchool.pojo.UserItem;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.Multipart;
import retrofit2.http.POST;
import retrofit2.http.Part;
import retrofit2.http.Query;

public class HttpBin {

    public static final String baseUrl = "http://192.168.43.130:2021/";
    public static final Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    public static final Service service = retrofit.create(Service.class);

    public interface Service {
        @GET("searchAll")
        Call<ActivityContainer> findActivity(@Query("uid") int uid, @Query("text") String text,
                                             @Query("school") String school, @Query("type") Integer type,
                                             @Query("sort") Integer sort, @Query("page") int page);

        @FormUrlEncoded
        @POST("login/")
        Call<Integer> login(@Field("name") String name, @Field("password") String password);

        @FormUrlEncoded
        @POST("register/")
        Call<Boolean> register(@Field("userName") String userName, @Field("password") String password,
                               @Field("realName") String realName, @Field("sid") String sid, @Field("school") String school);

        @FormUrlEncoded
        @POST("changePassword")
        Call<Boolean> changePassword(@Field("uid") int uid, @Field("oldPassword") String oldPassword, @Field("newPassword") String newPassword);

        @FormUrlEncoded
        @POST("changeInfo")
        Call<Boolean> changeInfo(@Field("realName") String realName, @Field("school") String school,
                                 @Field("userName") String userName, @Field("sid") String sid,
                                 @Field("profile") String profile, @Field("uid") int uid);

        @GET("searchMine")
        Call<ActivityContainer> findMyActivity(@Query("uid") int uid);

        @GET("searchActivityDetail")
        Call<ActivityDetail> searchOneActivity(@Query("uid") int uid, @Query("bid") int bid);

        @GET("sign")
        Call<Boolean> sign(@Query("uid") int uid, @Query("bid") int bid, @Query("reason") String reason);

        @GET("applyDetail")
        Call<ApplyDetail> applyDetail(@Query("uid") int uid, @Query("bid") int bid);

        @GET("choosePerson")
        Call<Boolean> choosePerson(@Query("owner") int owner, @Query("uid") int uid, @Query("bid") int bid);

        @GET("searchFriend")
        Call<List<UserItem>> searchFriend(@Query("uid") int uid);

        @GET("userDetail")
        Call<User> userDetail(@Query("uid") int uid);

        @GET("deleteFriend")
        Call<Boolean> deleteFriend(@Query("uid1") int uid1, @Query("uid2") int uid2);

        @GET("setUpActivity")
        Call<Boolean> setUpActivity(@Query("uid") int uid, @Query("type") int type, @Query("title") String title, @Query("text") String text);

        @GET("searchChat")
        Call<List<ChatItem>> searchChat(@Query("uid") int uid);

        @GET("receive")
        Call<List<MsgItem>> receiveChat(@Query("uid1") int uid1, @Query("uid2") int uid2);

        @GET("send")
        Call<Boolean> sendChat(@Query("uid1") int uid1, @Query("uid2") int uid2, @Query("text") String text);

        @Multipart
        @POST("uploadImage")
        Call<Boolean> uploadImage(@Part MultipartBody.Part part);
    }

    public interface ResponseListener<T> {
        void onResponse(Call<T> call, Response<T> response);

        void onFailure(Call<T> call, Throwable t);
    }


    public static void login(Context context, String name, String password, ResponseListener listener) {
        Call<Integer> call = service.login(name, password);
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void register(Context context, String userName, String password, String realName, String sid, String school, ResponseListener listener) {
        Call<Boolean> call = service.register(userName, password, realName, sid, school);
        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void findActivity(Context context, String text, String school, Integer type, Integer sort, Integer page, ResponseListener listener) {
        int uid = ((MainApplication) context.getApplicationContext()).getUid();
        Call<ActivityContainer> call = service.findActivity(uid, text, school, type, sort, page);
        call.enqueue(new Callback<ActivityContainer>() {
            @Override
            public void onResponse(Call<ActivityContainer> call, Response<ActivityContainer> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<ActivityContainer> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void findMyActivity(Context context, ResponseListener listener) {
        int uid = ((MainApplication) context.getApplicationContext()).getUid();
        Call<ActivityContainer> call = service.findMyActivity(uid);
        call.enqueue(new Callback<ActivityContainer>() {
            @Override
            public void onResponse(Call<ActivityContainer> call, Response<ActivityContainer> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<ActivityContainer> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void searchOneActivity(Context context, int bid, ResponseListener listener) {
        int uid = ((MainApplication) context.getApplicationContext()).getUid();
        Call<ActivityDetail> call = service.searchOneActivity(uid, bid);
        call.enqueue(new Callback<ActivityDetail>() {
            @Override
            public void onResponse(Call<ActivityDetail> call, Response<ActivityDetail> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<ActivityDetail> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void sign(Context context, int bid, String reason, ResponseListener<Boolean> listener) {
        int uid = ((MainApplication) context.getApplicationContext()).getUid();
        Call<Boolean> call = service.sign(uid, bid, reason);
        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void applyDetail(Context context, int uid, int bid, ResponseListener<ApplyDetail> listener) {
        Call<ApplyDetail> call = service.applyDetail(uid, bid);
        call.enqueue(new Callback<ApplyDetail>() {
            @Override
            public void onResponse(Call<ApplyDetail> call, Response<ApplyDetail> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<ApplyDetail> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void choosePerson(Context context, int uid, int bid, ResponseListener<Boolean> listener) {
        int owner = ((MainApplication) context.getApplicationContext()).getUid();
        Call<Boolean> call = service.choosePerson(owner, uid, bid);
        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void searchFriend(Context context, ResponseListener<List<UserItem>> listener) {
        int uid = ((MainApplication) context.getApplicationContext()).getUid();
        Call<List<UserItem>> call = service.searchFriend(uid);
        call.enqueue(new Callback<List<UserItem>>() {
            @Override
            public void onResponse(Call<List<UserItem>> call, Response<List<UserItem>> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<List<UserItem>> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void userDetail(Context context, int uid, ResponseListener<User> listener) {
        Call<User> call = service.userDetail(uid);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void deleteFriend(Context context, int uid, ResponseListener<Boolean> listener) {
        Call<Boolean> call = service.deleteFriend(((MainApplication) context.getApplicationContext()).getUid(), uid);
        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void setUpActivity(Context context, int type, String title, String text, ResponseListener<Boolean> listener) {
        int uid = ((MainApplication) context.getApplicationContext()).getUid();
        Call<Boolean> call = service.setUpActivity(uid, type, title, text);
        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void changePassword(Context context, String oldPassword, String newPassword, ResponseListener<Boolean> listener) {
        int uid = ((MainApplication) context.getApplicationContext()).getUid();
        Call<Boolean> call = service.changePassword(uid, oldPassword, newPassword);
        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void changeInfo(Context context, String realName, String school,
                                  String userName, String sid, String profile, ResponseListener<Boolean> listener) {
        int uid = ((MainApplication) context.getApplicationContext()).getUid();
        Call<Boolean> call = service.changeInfo(realName, school, userName, sid, profile, uid);
        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void searchChat(Context context, ResponseListener<List<ChatItem>> listener) {
        int uid = ((MainApplication) context.getApplicationContext()).getUid();
        Call<List<ChatItem>> call = service.searchChat(uid);
        call.enqueue(new Callback<List<ChatItem>>() {
            @Override
            public void onResponse(Call<List<ChatItem>> call, Response<List<ChatItem>> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<List<ChatItem>> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void receiveChat(Context context, int uid2, ResponseListener<List<MsgItem>> listener) {
        int uid1 = ((MainApplication) context.getApplicationContext()).getUid();
        Call<List<MsgItem>> call = service.receiveChat(uid1, uid2);
        call.enqueue(new Callback<List<MsgItem>>() {
            @Override
            public void onResponse(Call<List<MsgItem>> call, Response<List<MsgItem>> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<List<MsgItem>> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void sendChat(Context context, int uid2, String text, ResponseListener<Boolean> listener) {
        int uid1 = ((MainApplication) context.getApplicationContext()).getUid();
        Call<Boolean> call = service.sendChat(uid1, uid2, text);
        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }

    public static void uploadImage(Context context, String path, ResponseListener<Boolean> listener) {
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        File file = new File(path);
        RequestBody body = RequestBody.create(MediaType.parse("multipart/form-data"), file); 
        builder.addFormDataPart("image", file.getName(), body); 
        MultipartBody.Part part = builder.build().part(0);
        Call<Boolean> call = service.uploadImage(part);
        call.enqueue(new Callback<Boolean>() {
            @Override
            public void onResponse(Call<Boolean> call, Response<Boolean> response) {
                listener.onResponse(call, response);
            }

            @Override
            public void onFailure(Call<Boolean> call, Throwable t) {
                Toast.makeText(context, "网络错误", Toast.LENGTH_SHORT).show();
                listener.onFailure(call, t);
            }
        });
    }
}
